package net.pinnoocle.playadminpc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.admin.RegisterWechatEditParams;
import net.pinnoocle.playdao.entity.admin.RegisterWechatSearchParams;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.wechat.RegisterWechatListParams;
import net.pinnoocle.playdao.entity.wechat.TemplatedRaftListVo;
import net.pinnoocle.playservice.service.impl.RegisterWechatServiceImpl;
import net.pinnoocle.playservice.service.impl.WechatServiceImpl;
import net.pinnoocle.playservice.service.impl.WxServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ye
 * @Description
 * @create 2021-11-23 9:30
 */
@RestController
@RequestMapping("/wechat")
@Api(tags = "第三方平台")
@Slf4j
public class WechatController {

    @Autowired
    WechatServiceImpl wechatService;
    @Autowired
    RegisterWechatServiceImpl registerWechatService;
    @Autowired
    WxServiceImpl wxService;

    @GetMapping("registerList")
    @ApiOperation("申请注册的小程序列表")
    public BaseResult<PageModel<RegisterWechatListParams>> registerWechat(RegisterWechatSearchParams params){
        return new BaseResult<PageModel<RegisterWechatListParams>>().ok(registerWechatService.registerWechat(params));
    }

    @PostMapping("editCompanyInfo")
    @ApiOperation("微信审核失败时，修改公司信息，帮助他重新发起审核")
    public BaseResult<String> editCompanyInfo(@RequestBody RegisterWechatEditParams editParams){
        boolean result = registerWechatService.editCompanyInfo(editParams);
        if(result){
            return new BaseResult<String>().success("修改成功");
        }
        return new BaseResult<String>().error("修改失败");
    }

    @GetMapping("sendToWx")
    @ApiOperation("是否发送给微信审核 sendToWx：1 发送给微信审核 2 拒绝")
    public BaseResult<String> sendToWx(@RequestParam("registerId") String registerId,@RequestParam("sendToWx") Integer sendToWx){
        boolean result = registerWechatService.sendToWx(registerId, sendToWx);
        String resultMsg = (sendToWx == 1) ? "审核" : "拒绝";
        if(result){
            return new BaseResult<String>().success(resultMsg+"成功");
        }
        return new BaseResult<String>().error(resultMsg+"失败");
    }

    @GetMapping("againSendToWx")
    @ApiOperation("微信审核未通过，重新发送给微信审核（提示注意看微信审核结果）")
    public BaseResult<String> againSendToWx(@RequestParam("registerId") String registerId){
        boolean result = registerWechatService.againSendToWx(registerId);
        if(result){
            return new BaseResult<String>().success("申请成功，等待法人验证");
        }
        return new BaseResult<String>().error("申请失败");
    }

    @PostMapping("fastSetDefaultValue")
    @ApiOperation("一键设置新申请的小程序默认值")
    public BaseResult<String> fastSetDefaultValue(@RequestParam("registerId") String registerId) throws Exception {
        boolean result = wechatService.fastSetDefaultValue(registerId);
        if(result){
            return new BaseResult<String>().success("设置成功");
        }
        return new BaseResult<String>().error("设置失败");
    }

    @PostMapping("fastSetDefaultValueWithExists")
    @ApiOperation("一键设置已存在的小程序默认值")
    public BaseResult<String> fastSetDefaultValueWithExists(@RequestParam("shopId") String shopId,
                                                            @RequestParam("phone") String phone) throws Exception {
        boolean result = wechatService.fastSetDefaultValueWithExists(shopId,phone);
        if(result){
            return new BaseResult<String>().success("设置成功");
        }
        return new BaseResult<String>().error("设置失败");
    }

    @GetMapping("getAuthQrCode")
    @ApiOperation("获取授权地址(前端获取该地址后做跳转)")
    public BaseResult<String> getAuthQrCode(){
        return new BaseResult<String>().ok(wxService.getAuthQrCode());
    }

    @GetMapping("templatedRaftList")
    @ApiOperation("草稿箱列表")
    public BaseResult<List<TemplatedRaftListVo>> templatedRaftList(){
        List<TemplatedRaftListVo> templatedRaftListVos = wechatService.templatedRaftList();
        return new BaseResult<List<TemplatedRaftListVo>>().ok(templatedRaftListVos);
    }

    @GetMapping("addTemplate")
    @ApiOperation("草稿箱加入代码模板库")
    public BaseResult<String> addTemplate(@RequestParam("draftId") Integer draftId) {
        boolean result = wechatService.addTemplate(draftId);
        if (result) {
            return new BaseResult<String>().success("加入代码模板库成功");
        }
        return new BaseResult<String>().error("加入代码模板库失败");
    }
}
